package com.iob.coin.AOP;

import com.iob.coin.covid19kg.dataset.DatasetManagerLocalImpl;
import org.apache.jena.query.ReadWrite;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @Author: GuoXinyuan
 * @Date: 2021/6/10
 * TDB事务切面
 */

@Component
@Aspect
public class TDBTTransactionAspect {

    /**
     * 对每一个存取方法添加事务支持
     * @param point
     */
    @Around("execution(* com.iob.coin.covid19kg.dataset.DatasetManagerLocalImpl.*(..))")
    public Object transaction(ProceedingJoinPoint point) {
        DatasetManagerLocalImpl datasetManager = (DatasetManagerLocalImpl) point.getTarget();
        Object result  = null;
        datasetManager.getDataset().begin(ReadWrite.READ);
        try {
            result = point.proceed();
            datasetManager.getDataset().commit();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            datasetManager.getDataset().end();
        }
        return result;
    }

}
